package com.lej.core.utils.extention

fun <T> List<T>?.safe() = this ?: listOf()
fun <T> List<T>?.toArrayList() = ArrayList(this ?: listOf())


/**根据关键词模糊搜索对列表进行过滤处理*/
fun <T> List<T>.searchOfList(content: CharSequence?, block: T.() -> CharSequence): MutableList<T> {
    val tempList: MutableList<T> = ArrayList()
    if (content.isNullOrBlank()) {
        tempList.addAll(this)
        return tempList
    }
    val con = content.trim()
    var index: Int
    out@ for (element in this) {
        if (block(element).contains(con)) {
            tempList.add(element)
        } else {
            index = -1
            for (c in con) {
                index = block(element).indexOf(c, index + 1)
                if (index == -1) continue@out
            }
            tempList.add(element)
        }
    }
    return tempList
}